﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public static class AcQuyDAO
    {
        public static databaseDataContext _db = new databaseDataContext();

        //Hàm này lấy ra acquy có thông số Ah phù hợp
        public static object lay_danh_sach_acquy(float _ah)
        {
            var query = from aq in _db.ACQUYs
                        where aq.SoAH >= _ah
                        select aq;

            return query.ToList();
        }

        // hàm search ac quy
        public static List<SanPham_AcQuy_DTO> search_sanpham_acquy(string text)
        {
            List<SanPham_AcQuy_DTO> list = new List<SanPham_AcQuy_DTO>();
            var query = _db.ACQUYs;
            int stt = 1;

            foreach (var item in query)
            {
                SanPham_AcQuy_DTO acquy = new SanPham_AcQuy_DTO();
                if (item.TenSanPham.Contains(text) || item.SoAH.Value.ToString() == text || item.GiaTien.Value.ToString() == text)
                {
                    acquy.stt = stt;
                    acquy.id = item.MaAcQuy;
                    acquy.tensanpham = item.TenSanPham;
                    acquy.soAH = item.SoAH.HasValue ? float.Parse(item.SoAH.Value.ToString()) : 0;
                    acquy.giatien = item.GiaTien.HasValue ? float.Parse(item.GiaTien.Value.ToString()) : 0;

                    list.Add(acquy); stt++;
                }
            }

            return list;
        }

        public static void Insert_Sanpham_Acquy(SanPham_AcQuy_DTO acquy)
        {
            ACQUY _acquy = new ACQUY();
            _acquy.TenSanPham = acquy.tensanpham;
            _acquy.SoAH = acquy.soAH;
            _acquy.GiaTien = acquy.giatien;

            _db.ACQUYs.InsertOnSubmit(_acquy);
            _db.SubmitChanges();
        }

        public static void Delete_sanpham_acquy(int id)
        {
            var query = _db.ACQUYs.Single(p => p.MaAcQuy == id);
            _db.ACQUYs.DeleteOnSubmit(query);
            _db.SubmitChanges();
        }

        public static SanPham_AcQuy_DTO load_sanpham_acquy_id(int id)
        {
            SanPham_AcQuy_DTO acquy = new SanPham_AcQuy_DTO();
            var query = _db.ACQUYs.Single(p => p.MaAcQuy == id);

            acquy.tensanpham = query.TenSanPham;
            acquy.soAH = query.SoAH.HasValue ? float.Parse(query.SoAH.Value.ToString()) : 0;
            acquy.giatien = query.GiaTien.HasValue ? float.Parse(query.GiaTien.Value.ToString()) : 0;

            return acquy;
        }

        public static void Update_sanpham_acquy(SanPham_AcQuy_DTO acquy)
        {
            var item = _db.ACQUYs.Single(p => p.MaAcQuy == acquy.id);
            item.TenSanPham = acquy.tensanpham;
            item.SoAH = acquy.soAH;
            item.GiaTien = acquy.giatien;

            _db.SubmitChanges();
        }








    }
}
